﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraCharts;
using DevExpress.Utils;
using TierService;
using OrderMgr.SysSetting;
using System.Data.SqlClient;
using TierApplication;


namespace OrderMgr.OrderInfo
{
    public partial class frmBuyOrderSearchDetail : TierService.frmTemplate
    {
        #region "Member Variables"
        public string sFuncID;
        public TierService.Utility.OperateMode OpMode;
        public string sUserID;
        public string _OrderID = "";
        public Utility.OrderType _OrderType;
        public Utility.OrderStatus _AuditStatus;

        T_Order mOrder = new T_Order();
        SqlDataAdapter sdaOrderDetail = new SqlDataAdapter();
        DataSet dsOrderDetail = new DataSet();

        #endregion

        public frmBuyOrderSearchDetail()
        {
            InitializeComponent();
        }

        private void frmBuyDetail_Load(object sender, EventArgs e)
        {
            TierService.Utility.SetFeeVisible(clUnitPrice, TierService.Utility.gSeeBuyFeeData);
            TierService.Utility.SetFeeVisible(clSumPrice, TierService.Utility.gSeeBuyFeeData);
            TierService.Utility.SetFeeVisible(clOrderActMoney, TierService.Utility.gSeeBuyFeeData);

            initForm();

            switch (OpMode)
            {
                case Utility.OperateMode.Query:
                    showInfo(_OrderID);
                    break;
                case Utility.OperateMode.Insert:
                    break;
                case Utility.OperateMode.Update:

                    showInfo(_OrderID);
                    break;
                case Utility.OperateMode.Delete:
                    break;
                default:
                    break;
            }

            DataTable dt = TierService.Utility.InVisibleObject("iBuySearch");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string sControlID = dt.Rows[i]["ControlID"].ToString();
                if (sControlID == "tpBuyInvoice")
                    tpBuyInvoice.PageVisible = false;
                if (sControlID == "tpPayApp")
                    tpPayApp.PageVisible = false;
            }

        }

        private void initForm()
        {
            Utility.DataToLookupComboxEx(txtOrderType, C_DMB.GetListByType(C_DMB.DType.OrderType.ToString()), "CodeID", "CodeName");
            Utility.DataToLookupComboxEx(txtDepotID, C_DMB.GetListByType(C_DMB.DType.DepotType.ToString()), "CodeID", "CodeName");
            Utility.DataToLookupComboxEx(txtOrderUserID, (new P_User()).GetList(""), "UserID", "UserName");
            Utility.DataToLookupComboxEx(txtAuditUserID, (new P_User()).GetList(""), "UserID", "UserName");

            Utility.DataToLookupComboxEx(txtPayUserID, (new P_User()).GetList(""), "UserID", "UserName");
            Utility.DataToLookupComboxEx(txtSupplierID, (new T_Supplier()).GetList(""), "SupplierID", "SupplierName");
            Utility.DataToLookupComboxEx(txtPayType, C_DMB.GetListByType(C_DMB.DType.PayType.ToString()), "CodeID", "CodeName");
            //Utility.DataToLookupComboxEx(txtPayingUserID, (new P_User()).GetList(""), "UserID", "UserName");
            Utility.DataToLookupComboxEx(txtInvoiceUserID, (new P_User()).GetList(""), "UserID", "UserName");

            txtOrderUserID.Enabled = false;
            txtOrderType.Enabled = false;
            switch (_AuditStatus)
            {
                case Utility.OrderStatus.S0Input:
                    lblAuditDate.Visible = false;
                    lblAuditUserID.Visible = false;
                    txtAuditDate.Visible = false;
                    txtAuditUserID.Visible = false;
                    break;
                case Utility.OrderStatus.S1Auditing:
                    txtAuditDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
                    txtAuditUserID.EditValue = TierService.Utility.gUserID;
                    break;
                case Utility.OrderStatus.All:
                    break;
                default:
                    break;
            }


        }


        private void showInfo(string sID)
        {
            mOrder.GetModel(sID);
            mOrder.GetModel(sID);
            this.txtOrderID.Text = mOrder.OrderID;
            if (mOrder.OrderDate != null)
                this.txtOrderDate.DateTime = mOrder.OrderDate.Value;
            this.txtOrderUserID.EditValue = mOrder.OrderUserID;
            this.txtOrderType.EditValue = mOrder.OrderType;
            this.txtDepotID.EditValue = mOrder.DepotID;
            this.txtAuditUserID.EditValue = mOrder.AuditUserID;
            if (mOrder.AuditDate != null)
                this.txtAuditDate.DateTime = mOrder.AuditDate.Value;
            this.txtRemark.Text = mOrder.Remark;
            this.txtSupplierID.EditValue = mOrder.SupplierID;

            this.txtContractNo.Text = mOrder.ContractNo;
            this.txtPayType.EditValue = mOrder.PayType.ToString();
            if (mOrder.PayRstDate != null)
                this.txtPayRstDate.DateTime = mOrder.PayRstDate.Value;
            this.txtPayerFullName.Text = mOrder.PayerFullName;
            this.txtPayerBank.Text = mOrder.PayerBank;
            this.txtPayeBankAccount.Text = mOrder.PayeBankAccount;
            this.txtPayFee.Text = mOrder.PayFee.ToString();
            this.txtPayUserID.EditValue = mOrder.PayUserID;

            if (mOrder.PayAuditDate != null)
                txtPayAuditDate.DateTime = mOrder.PayAuditDate.Value;
            txtPayAuditUserID.EditValue = mOrder.PayAuditUserID;
            txtPayAuditOpinion.Text = mOrder.PayAuditOpinion;



            //*****************Order Detail*****************//
            BindOrderDetail();

        }


        private void BindOrderDetail()
        {
            gcBuyProduct.DataSource = T_Order.GetOrderDetailByOrderIDInt(_OrderID).Tables[0];

            T_Order mOrder = new T_Order();
            DataSet dsBuyStock = mOrder.GetBuyToStockList(" and a.POrderID='" + _OrderID + "'");
            gcBuyStock.DataSource = dsBuyStock.Tables[0];

            BindPayFeeList(_OrderID);
        }

        private void BindPayFeeList(string sOrderID)
        {
            T_OrderPayFee mOrderPayFee = new T_OrderPayFee();
            gcPayFee.DataSource = mOrderPayFee.GetPayFeeListByOrderID(sOrderID).Tables[0];
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            //Close();
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }


        private void btnOrderPrint_Click(object sender, EventArgs e)
        {
            string sOrderID = txtOrderID.Text;

            if (sOrderID != "")
            {
                TdsOrder dsOrder = TOrderFunc.GetOrderPrintList(sOrderID);
                RptOrderBuy xrptOrderBuy = new RptOrderBuy();
                xrptOrderBuy.DataSource = dsOrder;
                xrptOrderBuy.ShowPreviewDialog();
            }
        }


        private void btnBuyStockQuery_Click(object sender, EventArgs e)
        {
            if (gvBuyStock.RowCount < 1) return;
            DataRow dr = gvBuyStock.GetDataRow(gvBuyStock.FocusedRowHandle);
            string sID = dr["OrderID"].ToString();

            frmBuyToStockDetail fDetail = new frmBuyToStockDetail();
            fDetail.OpMode = TierService.Utility.OperateMode.Query;
            fDetail._AuditStatus = Utility.OrderStatus.S0Input;
            fDetail._OrderType = Utility.OrderType.BuyToStock;
            fDetail._OrderID = sID;
            fDetail.ShowDialog();
        }




    }
}